<?php
/**
 * Created by PhpStorm.
 * User: ouryun
 * Date: 2021/5/17
 * Time: 16:05
 */

namespace app\admin\controller;


use think\admin\Controller;
use think\App;

/**
 * 考勤记录管理
 * Class CheckWorkAttRecord
 * @package app\admin\controller
 */
class CheckWorkAttRecord extends Controller
{
    /**
     * 考勤记录表
     * @var string
     */
    protected $table = 'lg_check_work_face';

    /**
     * 店铺id
     * @var mixed|string
     */
    protected $shop_id = '';

    public function __construct(App $app)
    {
        parent::__construct($app);
        $this->shop_id = !empty(session('shop_info.shop_id')) ? session('shop_info.shop_id') : '';
    }

    /**
     * 考勤记录列表
     * @auth true
     * @login true
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function index()
    {
        $this->title = '考勤记录列表';
        $data = $this->request->param();
        //数据权限
        $map = $whereMap = [];
        if (!empty($this->shop_id)) {
            $map[] = ['id', 'IN', $this->shop_id];
            $whereMap[] = ['d.id', 'IN', $this->shop_id];
        }
        $this->shopList = $this->app->db->name('lg_distributor')->where('status', 1)->where($map)->field('id,name')->select()->toArray();
        $this->clerksList = $this->app->db->name('lg_shop_clerks')->where('status', 1)->field('id,name')->select()->toArray();
        $query = $this->_query($this->table)->alias('wf')
            ->join('lg_shop_clerks c', 'wf.clerks_id=c.id', 'LEFT')
            ->join('lg_distributor d', 'wf.shop_id=d.id', 'LEFT')
            ->join('lg_check_work_attendance wa', 'wf.check_work_id=wa.id', 'LEFT')
            ->field('wf.*,c.name clerks_name,d.name shop_name,wa.go_work_time,wa.off_work_time')
            ->order('wf.id DESC')
            ->where($whereMap);
        //搜索店员
        if (isset($data['clerks_id']) && $data['clerks_id'] !== '') {
            $query->where('wf.clerks_id', $data['clerks_id']);
        }
        //搜索店铺
        if (isset($data['shop_id']) && $data['shop_id'] !== '') {
            $query->where('wf.shop_id', $data['shop_id']);
        }
        //搜索考勤类型
        if (isset($data['type']) && $data['type'] !== '') {
            $query->where('wf.type', $data['type']);
        }
        $query->page();
    }

    /**
     * 处理列表数据
     * @param $list
     */
    protected function _page_filter(&$list)
    {
        foreach ($list as $key => $value) {
            if (empty($value['punch_date'])) {
                $punchData = date('Y-m-d');
            } else {
                $punchData = $value['punch_date'];
            }
            $list[$key]['go_work_time_temp'] = strtotime($punchData . ' ' . $value['go_work_time']);
            $list[$key]['off_work_time_temp'] = strtotime($punchData . ' ' . $value['off_work_time']);
        }
        foreach ($list as $k => $v) {
            if ($v['type'] == 1) {  //上班
                if ($v['punch_time'] > $v['go_work_time_temp']) {
                    $list[$k]['tt'] = '迟到';
                } else {
                    $list[$k]['tt'] = '正常';
                }
            } else {
                if ($v['punch_time'] < $v['off_work_time_temp']) {
                    $list[$k]['tt'] = '早退';
                } else {
                    $list[$k]['tt'] = '正常';
                }
            }
        }
    }
}